{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6ca2fb9c-183a-418e-a705-f7adc1bd0460",
   "metadata": {},
   "source": [
    "## Adding Callable Function Dependencies to an Electron\n",
    "\n",
    "Add functions to be invoked before or after executing an electron by using the electron's `call_before` and `call_after` key parameters. The functions, or *dependency calls*, are run in the electron's execution environment and can be used, for example, to set up and tear down resources for the electron's use.\n",
    "\n",
    "\n",
    "### Prerequisites\n",
    "\n",
    "Import `covalent` and `Path`.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "63e34368-1569-470b-8cf1-8df184d036dd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import covalent as ct\n",
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b303b508-5435-44f7-b635-b3b1080effe5",
   "metadata": {},
   "source": [
    "### Procedure\n",
    "\n",
    "1. Define functions to be executed before and after the electron:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d282be12-a87d-44cc-87a5-e95558300c37",
   "metadata": {},
   "outputs": [],
   "source": [
    "def call_before_hook(filename):\n",
    "    Path(filename).write_text('Hello world!')\n",
    "    return filename\n",
    "\n",
    "def call_after_hook(filename):\n",
    "    Path(filename).unlink()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ffee335-4b03-4c20-ad8b-7da7d921fbe4",
   "metadata": {},
   "source": [
    "2. Pass the functions as arguments to two Covalent dependency objects. The Covalent dependency class is `DepsCall()`.\n",
    "\n",
    "3. Assign the `call_before` and `call_after` named arguments in the electron decorator to reference the `DepsCall` objects.\n",
    "\n",
    "   `DepsCall` does not intrinsically distinguish between pre- and post-execution. The only difference is which keyword you assign the argument to."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "02c5fad7",
   "metadata": {},
   "outputs": [],
   "source": [
    "@ct.electron(\n",
    "    call_before=ct.DepsCall(call_before_hook, args=('test.txt',), retval_keyword='my_file'),\n",
    "    call_after=ct.DepsCall(call_after_hook, args=('test.txt',)),\n",
    ")\n",
    "def read_from_file(my_file=None):\n",
    "    with open(my_file,'r') as f:\n",
    "        return f.read()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9bc006e",
   "metadata": {},
   "source": [
    "4. Create and dispatch a lattice containing the electron:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "af4d80cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello world!\n"
     ]
    }
   ],
   "source": [
    "@ct.lattice\n",
    "def workflow():\n",
    "    return read_from_file()\n",
    "\n",
    "dispatch_id = ct.dispatch(workflow)()\n",
    "r = ct.get_result(dispatch_id, wait=True)\n",
    "print(r.result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eef829e4",
   "metadata": {},
   "source": [
    "### See Also\n",
    "\n",
    "[Adding Pip Dependencies to an Electron](./add_pip_dependencies_to_electron.ipynb)\n",
    "\n",
    "[Adding Bash Dependencies to an Electron](./add_bash_dependencies_to_electron.ipynb)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "vscode": {
   "interpreter": {
    "hash": ""
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
